// pagesCollect/pages/collect/perfect/perfect.js
import { getVillageDetail } from "../../../../api/collect"
import { baseUrl } from "../../../../api/request"

const app = getApp()

const way = {
  0: { name: '景区', key: 'scenic' },
  1: { name: '饭店', key: 'hotel' },
  2: { name: '民宿', key: 'homestay' },
  3: { name: '特产', key: 'speciality' }
}

Page({
  data: {
    way:way,
    villageId: null,
    list: [],
    navData: {
      title: '信息完善',
      level: 10
    },
    navHeight: 32,
    navTop: 48,
    triggered: false
  },
  onLoad(e) {
    this.setData({
      ['navData.title']: e.name ? (e.name + '信息完善') : '信息完善',
      navHeight: app.globalData.navHeight, //导航栏高度
      navTop: app.globalData.navTop, //导航栏距顶部距离
    });

    this.data.villageId = e.id;
    this.refresh()
  },
  getlist() {
    let { scroll, list } = this.data;
    getVillageDetail({
      villageId: this.data.villageId
    }).then(res => {
      let list = res.data.rows;
      list.forEach(item => {
        if (item.image) {
          item.image = baseUrl + item.image.split(',')[0];
        }
      })
      this.setData({
        triggered: false,
        list
      })
    }, function (err) {
    })
  },
  refresh() {
    this.setData({
      list: []
    })
    this.getlist()
  },
  openInfo(e) {
    let index = e.currentTarget.dataset.index;
    let data = this.data.list[index];
    // if (data.submitState == 1) {
    //   return false;
    // }
    wx.navigateTo({
      url: `/pagesCollect/pages/collect/edit/${way[data.type].key}/${way[data.type].key}?id=${data.id}&villageId=${this.data.villageId}`,
    })
  },
  addData() {
    let itemlist = [];
    for (const key in way) {
      itemlist.push('添加' + way[key].name)
    }
    wx.showActionSheet({
      itemList: itemlist,
      itemColor: "#666",
      success:  (res)=> {
        if (!res.cancel) {
          let wayData = way[res.tapIndex];
          wx.navigateTo({
            url: `/pagesCollect/pages/collect/edit/${wayData.key}/${wayData.key}?villageId=${this.data.villageId}`,
          })
        }
      }
    })
  },
})